package cn.xjfcloud.onedriveserver.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.xjfcloud.onedriveserver.pojo.dto.ApiConfig;
import cn.xjfcloud.onedriveserver.pojo.dto.OnedriveFileInfo;
import cn.xjfcloud.onedriveserver.pojo.dto.UploadInfo;
import cn.xjfcloud.onedriveserver.service.ApiConfigService;
import cn.xjfcloud.onedriveserver.service.UploadFileService;
import cn.xjfcloud.onedriveserver.util.ApiUtil;
import cn.xjfcloud.onedriveserver.util.UploadFileUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
 * 删除文件
 * @author xjf
 *
 */
@RestController
public class DeleteFileController {
	
	@Autowired
	private UploadFileService uploadFileService;
	@Autowired
	private ApiConfigService apiConfigService;
	
	public final static Logger LOG = LoggerFactory.getLogger(DeleteFileController.class);
	
	/**
	 * 删除onedrive文件
	 * @param fileId
	 * @return
	 */
	@ApiOperation(value = "删除文件", notes = "删除文件")
	@RequestMapping("deleteFile/{fileId}")
	public Map<String, Object> deleteFile(@ApiParam@PathVariable("fileId") String fileId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		OnedriveFileInfo onedriveFileInfo = uploadFileService.getOnedriveFileInfoByFileId(fileId);
		//判断是否上传完成
		if(onedriveFileInfo.getFileSize() == null) {
			//未完成上传
			resultMap.put("code", "10001");
			resultMap.put("msg", "file not had uploaded");
			return resultMap;
		}else {
			if(uploadFileService.deleteOnedriveFile(fileId)) {
				resultMap.put("code", "0");
				resultMap.put("msg", "delete onedrive file success");
				return resultMap;
			}else {
				resultMap.put("code", "10002");
				resultMap.put("msg", "delete onedrive file fail");
				return resultMap;
			}
		}
	}
}
